package com.shanghaionstar.weight;

import android.content.Context;
import android.graphics.drawable.AnimationDrawable;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;

import com.onstar.cn.common.log.LogManager;
import com.onstar.cn.weight.ptr.PtrFrameLayout;
import com.onstar.cn.weight.ptr.PtrIndicator;
import com.onstar.cn.weight.ptr.PtrUIHandler;
import com.shanghaionstar.R;

/**
 * Created by Yonglang Hong on 2016/8/29.
 */
public class OnStarPtrHeader extends RelativeLayout implements PtrUIHandler {

    private AnimationDrawable animationDrawable;

    public OnStarPtrHeader(Context context) {
        super(context);
        initView();
    }

    public OnStarPtrHeader(Context context, AttributeSet attrs) {
        super(context, attrs);
        initView();
    }

    public OnStarPtrHeader(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initView();
    }

    private void initView() {
        View header = LayoutInflater.from(getContext()).inflate(R.layout.head_carstatus_loading, this);
        ImageView img_loading = (ImageView) header.findViewById(R.id.img_loading);
        animationDrawable = (AnimationDrawable) img_loading.getDrawable();
    }

    @Override
    public void onUIReset(PtrFrameLayout frame) {

    }

    @Override
    public void onUIRefreshPrepare(PtrFrameLayout frame) {
//        description.setText("下拉可以刷新!");
//        updated_at.setText(getResources().getString(com.onstar.cn.weight.R.string.updated_at,"2天"));
        start();
        LogManager.getInstance().d("onUIRefreshPrepare");
    }

    @Override
    public void onUIRefreshBegin(PtrFrameLayout frame) {
        start();
        LogManager.getInstance().d("onUIRefreshBegin");
    }

    private void start(){
        if (animationDrawable != null && !animationDrawable.isRunning()){
            animationDrawable.start();
        }
    }

    private void stop(){
        if (animationDrawable != null && animationDrawable.isRunning()){
            animationDrawable.stop();
        }
    }

    @Override
    public void onUIRefreshComplete(PtrFrameLayout frame) {
        stop();
    }

    @Override
    public void onUIPositionChange(PtrFrameLayout frame, boolean isUnderTouch, byte status, PtrIndicator ptrIndicator) {

    }
}
